import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../pages/Home'
import Login from '../pages/Login'
import Register from '../pages/Register'
import Search from '../pages/Search'
import Test from '../pages/Test'

Vue.use(VueRouter)

// 第一步：保存一份最原始的push和replace
const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace

// 第二步：修改VueRouter.prototype上的push
VueRouter.prototype.push = function(location,okCallback,errCallback){
	// 第三步：追加自己的逻辑
	if(okCallback && errCallback){ //如果使用者传递了成功的回调、失败的回调，那么就直接传递就ok了
		return originPush.call(this,location,okCallback,errCallback)
	}else{
		return originPush.call(this,location).catch(()=>{})
	}
}
// 第二步：修改VueRouter.prototype上的push
VueRouter.prototype.replace = function(location,okCallback,errCallback){
	// 第三步：追加自己的逻辑
	if(okCallback && errCallback){ //如果使用者传递了成功的回调、失败的回调，那么就直接传递就ok了
		return originReplace.call(this,location,okCallback,errCallback)
	}else{
		return originPush.call(this,location).catch(()=>{})
	}
}

export default new VueRouter({
	mode:'history',
	routes:[
		{
			path:'/home',
			component:Home
		},
		{
			path:'/login',
			component:Login
		},
		{
			path:'/register',
			component:Register
		},
		{
			path:'/search',
			component:Search
		},
		{
			path:'/test',
			component:Test
		},
		{
			path:'/',
			redirect:'/home'
		}
	]
})
